//
//  DepositListController.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2020/3/16.
//  Copyright © 2020 zwb. All rights reserved.
//

import UIKit


class ZyaDepositListVC: ZyaTenderBaseTableVC {
    
    var dataArray = [ZyaDepositModel]()
    var auditMark = "0" //      0:待审核;1:已审核;2:退款中;3:已退款
    var searchTitle = ""
    var page = 1
    let helper = ZyaTenderHelper()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9607843137, blue: 0.9607843137, alpha: 1)
        tableView.register(UINib(nibName: ZyaDepositListCell.identifier, bundle: nil), forCellReuseIdentifier: ZyaDepositListCell.identifier)
        tableView.tableFooterView = UIView()
        
        tableView.mj_header = KSRefreshHeader {
            self.page = 1
            self.network()
        }
        tableView.mj_footer = KSRefreshFooter {
            self.page += 1
            self.network()
        }
        tableView.mj_header?.beginRefreshing()
        
    
        
        
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
        
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaDepositListCell.identifier, for: indexPath) as? ZyaDepositListCell else {
            fatalError("cell error")
        }
        let one = self.dataArray[indexPath.row]
        cell.one = one
        cell.obClickAction = { [one] title in
           if title == "退款" || title == "取消退款" {
                alertUtil(self, title: "确认\(title)?", operateS: [nameAction(name: "确定", action: { (_) in
                    KSNetwork.shared.doRequest(API.refundMyDepositInfo(id_key: one.idKey.unwrap, opt_type: title == "退款" ? "1" : "0")) { (data:BaseResult<OperateResult>) in
                        if data.flag == TRUE {
                            KSToast("操作成功")
                            self.tableView.mj_header?.beginRefreshing()
                        } else {
                            KSToast(data.message)
                        }
                    }
                })])
                
                
            } else {
                print("33")
            }
            
        }
        return cell
    }
    func network () {
        KSNetwork.shared.doRequest(API.queryMyDepositList(z_staff_id: USERINFO.usrid.unwrap, audit_mark: auditMark, search_info: searchTitle,page:"\(page)",size:"10"),showHud: false) { (data:BaseResult<ZyaDepositModel>) in
            
            if data.flag == TRUE {
                if let infos = data.infos {
                    self.dataArray.removeAll()
                    self.dataArray += infos
                    
                    
                    
                    self.tableView.mj_header?.endRefreshing()
                    if infos.count < 10 {
                        self.tableView.mj_footer?.endRefreshingWithNoMoreData()
                    } else {
                        self.tableView.mj_footer?.endRefreshing()
                    }
                    self.tableView.reloadData()
                    
                }
                
            } else {
                KSToast(data.message)
                self.tableView.mj_footer?.endRefreshing()
            }
            
            
        }
        
    }
    
    
    
    
}
